/**
 * 接口：用户、登录
 * @Author: 
 * @Date: 
 * @Copyright：
 */
import { postRequest, getRequest } from '/@/api/config/axios-fun';
import { SYSTEM_MODULES_MAP } from '../../constants/system.js';
import { getAppInfo } from "/@/utils/storage";
import { APP_CODE } from '../../constants/keys.js';
import { ApiUtil } from "/@/utils/api.js";

export const UserAPI = {
    init: () => ({
        id: 0,
        appUserId: null,
        appId: null,
        organizeId: null,
        organizeName: null,
        account: null,
        password: null,
        name: null,
        mail: null,
        phone: null,
        qQ: null,
        groupId: null,
        birth: null,
        educationId: null,
        departmentId: null,
        postId: null,
        quality: null,
        workTypeId: null,
        workLicenseFlag: null,
        aliasName: null,
        accountValidTime: null,
        accountValidFlag: null,
        passwordValidTime: null,
        passwordValidFlag: null,
        signature: null,
    }),
    loginUser(params = {}) {
        // return new Promise((resolve) => {
        // 登录和登出传模块，可以多设备登录
        if (!params.module) {
            params.module = SYSTEM_MODULES_MAP.PC;
        }
        if (!params) {
            params = { appId: getAppInfo().id };
        } else if (!Object.prototype.hasOwnProperty.call(params, 'appId')) {
            params.appId = getAppInfo().id;
        }
        params.appCode = APP_CODE;
        return postRequest("user/loginUser", params, { formDataFlag: true, inDataFlag: false, noToken: true });
        // })
    },
    getUser(params = {}) {
        return new Promise((resolve) => {
            getRequest("user/getUser", params, { formDataFlag: true, inDataFlag: false }).then(({ res, data }) => {
                let item = {};
                if (ApiUtil.isResponseSuccess(data)) {
                    item = data.data;
                }
                resolve({ res, data, item });
            }).catch((error) => {
                console.log(error);
            });
        });

    },
    listUser(params) {
        if (!params.appId) {
            params.appId = getAppInfo().id;
        }
        return new Promise((resolve) => {
            getRequest("user/listUser", params).then(({ res, data }) => {
                let list = [],
                    pageIndex = 1,
                    count = 0;
                if (ApiUtil.isResponseSuccess(data)) {
                    list = data.data.data;
                    if (data.data) {
                        pageIndex = data.data.currentPage;
                        count = data.data.count;
                    }
                }
                resolve({ res, data, list, pageIndex, count });
            });
        });
    },

    insertUser(params, showMsgFlag = true, msg = '新增') {
        return new Promise((resolve) => {
            postRequest('user/insertUser', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    updateUser(params, showMsgFlag = true, msg = '修改') {
        return new Promise((resolve) => {
            postRequest('user/updateUser', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    deleteUser(params, showMsgFlag = true, msg = '删除') {
        return new Promise((resolve) => {
            postRequest('user/deleteUser', params, { inDataFlag: false }).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    updateUserPassword(params, oldpwd, showMsgFlag = true, msg = '修改') {
        return new Promise((resolve) => {
            postRequest('user/updateUserPassword', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    updateUserResetPassword(params, showMsgFlag = true, msg = '重置') {
        return new Promise((resolve) => {
            postRequest('user/updateUserResetPassword', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    systemSwitch(params, showMsgFlag = false, msg = '切换') {
        return new Promise((resolve) => {
            postRequest('user/systemSwitch', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    logout(params = {}) {
        return new Promise((resolve) => {
            if (!params.module) {
                params.module = SYSTEM_MODULES_MAP.PC;
            }
            postRequest('user/logout', params).then(({ res, data }) => {
                resolve({ res, data });
            });
        });
    },

    onlineList(params = {}) {
        return new Promise((resolve) => {
            if (!params.module) {
                params.module = SYSTEM_MODULES_MAP.PC;
            }
            if (!params) {
                params = { appId: getAppInfo().id };
            } else if (!Object.prototype.hasOwnProperty.call(params, 'appId')) {
                params.appId = getAppInfo().id;
            }
            getRequest('/user/onlineList', params).then(({ res, data }) => {
                let list = [],
                    pageIndex = 1,
                    count = 0;
                if (ApiUtil.isResponseSuccess(data)) {
                    list = data.data.data;
                    if (data.data) {
                        pageIndex = data.data.currentPage;
                        count = data.data.count;
                    }
                }
                resolve({ res, data, list, pageIndex, count });
            });
        });
    },

    insertUserAndRole(params, showMsgFlag = true, msg = '新增') {
        return new Promise((resolve) => {
            postRequest('user/insertUserAndRole', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    updateUserAndRole(params, showMsgFlag = true, msg = '新增') {
        return new Promise((resolve) => {
            postRequest('user/updateUserAndRole', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },
    listUserNew(params) {
        return new Promise((resolve) => {
            getRequest('/user/listUserNew', params).then(({ res, data }) => {
                let list = [],
                    pageIndex = 1,
                    count = 0;
                if (ApiUtil.isResponseSuccess(data)) {
                    list = data.data.data;
                    if (data.data) {
                        pageIndex = data.data.currentPage;
                        count = data.data.count;
                    }
                }
                resolve({ res, data, list, count });
            });
        });
    },
    getCaptcha(params = {}) {
        return new Promise((resolve) => {
            params.notToken = true;
            getRequest('/user/getCaptcha', params).then((resp) => {
                resolve({ ...ApiUtil.cleanGet(resp) });
            });
        });
    },

}